home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Open Transport / Sample Code / NetFractal™ / ES stuff□ / InPoint.cpp < prev    next >
Encoding:
Text File  |  1996-11-19  |  1.4 KB  |  106 lines  |  [TEXT/MPCC]

  1. // InPoint.cpp
  2.  
  3. #include "InPoint.h"
  4.  
  5. #define TIMEOUT 2000
  6.  
  7.  
  8.  
  9. InPoint::InPoint(
  10.     const char *protocol,
  11.     const char *address,
  12.     short port)
  13. {
  14.     OSErr err = Open(protocol);
  15.     if (err)
  16.         throw err;
  17.  
  18.     err = Bind(port);
  19.     if (err)
  20.         throw err;
  21.  
  22.     // make it asynchronous and set up to receive
  23.  
  24.     OTSetAsynchronous(fEndpoint);
  25.     OTInstallNotifier(fEndpoint, &Notifier, this);
  26. }
  27.  
  28.  
  29. InPoint::~InPoint()
  30. {
  31. }
  32.  
  33.  
  34.  
  35. pascal void
  36. InPoint::Notifier(
  37.     void* contextPtr,
  38.     OTEventCode code, 
  39.     OTResult result,
  40.     void* cookie)
  41.  
  42.     // *** Called at deferred task time. ***
  43.  
  44. {
  45.     InPoint* thisPoint = (InPoint*) contextPtr;
  46.     thisPoint->HandleNotify(code, result, cookie);
  47. }
  48.  
  49.  
  50. void
  51. InPoint::HandleNotify(
  52.     OTEventCode code, 
  53.     OTResult result,
  54.     void* cookie)
  55.  
  56.     // *** Called at deferred task time. ***
  57.  
  58. {
  59.     switch (code) {
  60.         case T_DATA:
  61.             GetData();
  62.             break;
  63.         
  64.         
  65.  
  66.     }
  67. }
  68.  
  69.  
  70. void
  71. InPoint::GetData()
  72.  
  73. {
  74.     unsigned char foo[256];
  75.     while (1) {
  76.         TUnitData uData = {
  77.             { 256, 0, foo },
  78.             { 0, 0, NULL },
  79.             { sizeof(packetBuffer), 0, (UInt8*) &fPacket },
  80.         };
  81.         OTFlags flags;
  82.         
  83.         OSStatus theErr = OTRcvUData(fEndpoint, &uData, &flags);
  84.         if (theErr == kOTNoDataErr)
  85.             break;
  86.         if (theErr != noErr) {
  87.             Str15 s;
  88.             NumToString(theErr, s);
  89.             DebugStr(s);
  90.             continue;
  91.         }
  92.  
  93.         DoSomethingWithTheData(&fPacket, uData.udata.len);
  94.     }    
  95. }
  96.  
  97.  
  98. void
  99. InPoint::DoSomethingWithTheData(
  100.     void *data,
  101.     long len)
  102. {
  103.     // do nothing in this class - please override
  104. }
  105.  
  106.